package jfood.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.text.DefaultCaret;

import jfood.waiter.WaiterAgent;

public class WaiterGui extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1927808120588666590L;
	private JTextField messageBox;
	private JTextArea chatBox;
	private WaiterAgent agent;
	private boolean blocked;

	public WaiterGui(int waiterNumber, WaiterAgent agent) {
		this.agent = agent;
		blocked = false;
		this.setTitle("Kelner nr " + waiterNumber);
        JPanel southPanel = new JPanel();

        this.add(southPanel, BorderLayout.SOUTH);
        southPanel.setBackground(Color.BLUE);
        southPanel.setLayout(new GridBagLayout());

        messageBox = new JTextField(30);
        JButton sendMessage = new JButton("Send Message");
        chatBox = new JTextArea();
        chatBox.setEditable(false);
 
        this.add(new JScrollPane(chatBox), BorderLayout.CENTER);

        chatBox.setLineWrap(true);

        GridBagConstraints left = new GridBagConstraints();
        left.weightx = 1.0;
        left.fill = GridBagConstraints.HORIZONTAL;
        GridBagConstraints right = new GridBagConstraints();

        southPanel.add(messageBox, left);
        southPanel.add(sendMessage, right);

        sendMessage.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent arg0) {
				WaiterGui.this.acceptMessage();
			}
		});
        
        sendMessage.registerKeyboardAction(sendMessage.getActionForKeyStroke(
                KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0, false)),
                KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, false),
                JComponent.WHEN_FOCUSED);
        
        sendMessage.registerKeyboardAction(sendMessage.getActionForKeyStroke(
                KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0, true)),
                KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, true),
                JComponent.WHEN_FOCUSED);
        
        messageBox.addKeyListener(new KeyListener() {
			
			public void keyTyped(KeyEvent arg0) {
				//Do nothing
			}
			
			public void keyReleased(KeyEvent arg0) {
				//Do nothing
			}
			
			public void keyPressed(KeyEvent arg0) {
				//Do nothing
				if (arg0.getKeyCode() == KeyEvent.VK_ENTER)
				{
					WaiterGui.this.acceptMessage();
				}
			}
		});
        
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        this.setSize(470, 300);
        
		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent we) {
				WaiterGui.this.agent.regService();
			}
		});
	}
	
	public void printMessage(String message){
		if (!blocked) {
			return;
		}
        if (message.length() < 1) {
            // do nothing 
        } else {
            chatBox.append(message + "\n");
            chatBox.setCaretPosition(chatBox.getDocument().getLength());
            messageBox.setEnabled(true);
            blocked = false;
        }
	}
	
	public void acceptMessage(){
		if (blocked) {
			return;
		}
		
        if (messageBox.getText().length() < 1) {
            // do nothing 
        } else {
            chatBox.append(messageBox.getText() + "\n");
            chatBox.setCaretPosition(chatBox.getDocument().getLength());
            agent.askForMeal(messageBox.getText());
            messageBox.setText("");
            messageBox.setEnabled(false);
            blocked = true;
        }
	}
}

